home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / ASM 2.0 ƒ / util.c < prev   
Encoding:
C/C++ Source or Header  |  1992-06-15  |  3.3 KB  |  227 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "as.h"
  4.  
  5. extern char *File_name;
  6. extern int Line_num;
  7. extern Err_count;
  8. extern char *Optr;
  9.  
  10. /*
  11.  *      fatal --- fatal error handler
  12.  */
  13. fatal(str)
  14. char    *str;
  15. {
  16.     printf("%s\n",str);
  17.     exit(-1);
  18. }
  19.  
  20. /*
  21.  *      error --- error in a line
  22.  *                      print line number and error
  23.  */
  24. error(str)
  25. char    *str;
  26. {
  27.     printf("%s,%d: %s\n",File_name,Line_num,str);
  28.     Err_count++;
  29. }
  30.  
  31. /*
  32.  *      serror --- error in a line
  33.  *                      print line number and error
  34.  */
  35. serror(fmt,s)
  36. char    *fmt;
  37. char    *s;
  38. {
  39.     printf("%s,%d: ",File_name,Line_num);
  40.     printf(fmt,s);
  41.     printf("\n");
  42.     Err_count++;
  43. }
  44. /*
  45.  *      warn --- trivial error in a line
  46.  *                      print line number and error
  47.  */
  48. warn(str)
  49. char    *str;
  50. {
  51.     extern    int    Pass;
  52.  
  53.     if(Pass==1)return;    /* save warnings till second pass */
  54.     printf("%s,%d: ",File_name,Line_num);
  55.     printf("Warning --- %s\n",str);
  56. }
  57.  
  58.  
  59. /*
  60.  *      delim --- check if character is a delimiter
  61.  */
  62. delim(c)
  63. char    c;
  64. {
  65.     return( isspace(c) );
  66. }
  67.  
  68. /*
  69.  *      skip_white --- move pointer to next non-whitespace char
  70.  */
  71. char *skip_white(ptr)
  72. char    *ptr;
  73. {
  74.     while(*ptr==' ' || *ptr=='\t')
  75.         ptr++;
  76.     return(ptr);
  77. }
  78.  
  79. /*
  80.  *      any --- does str contain c?
  81.  */
  82. any(c,str)
  83. char    c;
  84. char    *str;
  85. {
  86.     while(*str)
  87.         if(*str++ == c)
  88.             return(1);
  89.     return(0);
  90. }
  91.  
  92. /*
  93.  *      mapdn --- convert A-Z to a-z
  94.  */
  95. char mapdn(c)
  96. char c;
  97. {
  98.     return( isupper(c) ? tolower(c) : c);
  99. }
  100.  
  101. /*
  102.  *      loword --- return low word of an int
  103.  */
  104. loword(i)
  105. int i;
  106. {
  107.     return(i&0xFFFF);
  108. }
  109.  
  110. /*
  111.  *      hiword --- return high word of an int
  112.  */
  113. hiword(i)
  114. int i;
  115. {
  116.     return((i>>16)&0xFFFF);
  117. }
  118.  
  119. /*
  120.  *      lobyte --- return low byte of an int
  121.  */
  122. lobyte(i)
  123. int i;
  124. {
  125.     return(i&0xFF);
  126. }
  127.  
  128. /*
  129.  *      hibyte --- return high byte of a short int
  130.  */
  131. hibyte(i)
  132. int i;
  133. {
  134.     return((i>>8)&0xFF);
  135. }
  136.  
  137. /*
  138.  *      head --- is str2 the head of str1?
  139.  */
  140. head(str1,str2)
  141. char *str1,*str2;
  142. {
  143.     while( *str1 && *str2 ){
  144.         if( *str1 != *str2 )break;
  145.         str1++;
  146.         str2++;
  147.         }
  148.     if(*str1 == *str2)return(1);
  149.     if(*str2=='\0')
  150.         if( any(*str1," \t\n,+-];*.") )return(1);
  151.     return(0);
  152. }
  153.  
  154. /*
  155.  *      alpha --- is character a legal letter
  156.  */
  157. alpha(c)
  158. char c;
  159. {
  160.     return( isalpha(c) || c== '_');
  161. }
  162. /*
  163.  *      alphan --- is character a legal letter or digit
  164.  */
  165. alphan(c)
  166. char c;
  167. {
  168.     return( alpha(c) || isdigit(c) || c=='$');
  169. }
  170.  
  171. /*
  172.  *    white --- is character whitespace?
  173.  */
  174. white(c)
  175. char c;
  176. {
  177.     return( c == '\t' || c == ' ' || c == '\n' );
  178. }
  179.  
  180. /*
  181.  *      reverse --- reverse the bits in an int
  182.  *
  183.  *      Algorithm from Dr. Dobbs Journal #46, June/July 1980, p.48
  184.  *      Original by C. Strachey [CACM 4:3 961 p.146]
  185.  */
  186. reverse(val)
  187. int val;
  188. {
  189.     static int mask[] = { 0x55555555, 0x33333333,
  190.                   0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF };
  191.     register int i = val;
  192.     register int j = 16;
  193.     register int k = 4;
  194.  
  195.     while(j){
  196.         i = ((i&mask[k])<<j)|((i>>j)&mask[k]);
  197.         j >>= 1;
  198.         k--;
  199.         }
  200.     return(i);
  201. }
  202.  
  203. /*
  204.  *      next_str --- get next string from Operand field
  205.  *
  206.  *      The string is copied into a private buffer that gets
  207.  *      overwritten on each call to next_str.  Returns NULL
  208.  *      if no arguments are left.
  209.  */
  210. char *
  211. next_str()
  212. {
  213.     static char nsbuf[100];
  214.     char *p = nsbuf;
  215.  
  216.     while( *Optr ){
  217.         if( *Optr == ',' || *Optr == '\n'){
  218.             Optr++;
  219.             *p = '\0';
  220.             return(nsbuf);
  221.             }
  222.         else
  223.             *p++ = *Optr++;
  224.         }
  225.     return(NULL);
  226. }
  227.